﻿/* GLOBAL VARIABLES */
var totalGamer = 4;
var round=0; //Vong danh
var manualState=0; //fom ha bang tay

var colorB = '#000' ;
var colorR = '#D40000';
var srcClub = 'image/c.png';
var srcDiamond = 'image/d.png';
var srcHeart = 'image/h.png';
var srcSpade = 'image/s.png';
var srcBackCard = 'image/matsau.png';

var arrayNum = new Array();
arrayNum[0] = [0,null];
arrayNum[1] = [1,"A"];
arrayNum[2] = [2,"2"];
arrayNum[3] = [3,"3"];
arrayNum[4] = [4,"4"];
arrayNum[5] = [5,"5"];
arrayNum[6] = [6,"6"];
arrayNum[7] = [7,"7"];
arrayNum[8] = [8,"8"];
arrayNum[9] = [9,"9"];
arrayNum[10] = [10,"10"];
arrayNum[11] = [11,"J"];
arrayNum[12] = [12,"Q"];
arrayNum[13] = [13,"K"];

var arraySuit = new Array();
arraySuit[0] = [0,srcBackCard,null];
arraySuit[1] = [1,srcSpade,colorB];
arraySuit[2] = [2,srcClub,colorB];
arraySuit[3] = [3,srcDiamond,colorR];
arraySuit[4] = [4,srcHeart,colorR];

function objCard(_numId, _suitId){
	var j=null, k=null;
	for(var i=0;i<arrayNum.length;i++){
		if(_numId==arrayNum[i][0]){
			j = i;
			break;
		}
	}
	for(var i=0;i<arraySuit.length;i++){
		if(_suitId==arraySuit[i][0]){
			k = i;
			break;
		}
	}
	this.num = arrayNum[j];
	this.suit = arraySuit[k];
	this.click = 0; //1: click
	this.checkH = false; //check[true]: nam trong fom ngang
	this.checkV = false; //check[true]: nam trong fom doc	
	this.take = false; //take[true]: quan bai an
	return true;
}

function objPlayer(_name,_mark,_ip){
	this.name=_name;
	this.mark=_mark;
	this.ip=_ip;
	this.isReady=false;
	this.isCreateTbl=false;	
	this.arrCard = [];
	this.arrCardDown = [];
	this.arrCardGamble = [];
	
	//khoi tao mang card tren tay khi vao bat dau choi
	this.initCard = function(_arrNumId, _arrSuitId) {
		for(var i=0;i<_arrNumId.length;i++){
			this.arrCard[i] = new objCard(_arrNumId[i], _arrSuitId[i]);
		}
	};
	//click card tren tay
	this.setClick = function(_id, _option){
		this.arrCard[_id].click = _option;
	};
	this.addCard = function(_objCard){
		this.arrCard.push(_objCard);
	};
	this.removeCard = function(_index){
		this.arrCard.splice(_index,1);
	};
	// true[false] : sap xep theo so[chat]
	this.flagSort = null;
	this.sort = function(_flagSort){
		if(_flagSort == true){
			for(var i=0; i<this.arrCard.length; i++){
				for(var j=i+1;j<this.arrCard.length;j++){
					if(parseInt(this.arrCard[j].num[0]) < parseInt(this.arrCard[i].num[0])){
						var temp = this.arrCard[i];
						this.arrCard[i] = this.arrCard[j];
						this.arrCard[j] = temp;
					}
					if(parseInt(this.arrCard[j].num[0]) == parseInt(this.arrCard[i].num[0])){
						if(parseInt(this.arrCard[j].suit[0]) < parseInt(this.arrCard[i].suit[0])){
							var temp = this.arrCard[i];
							this.arrCard[i] = this.arrCard[j];
							this.arrCard[j] = temp;
						}
					}
				}
			}
			this.flagSort = false;
		}else{
			for(var i=0; i<this.arrCard.length; i++){
				for(var j=i+1;j<this.arrCard.length;j++){
					if(parseInt(this.arrCard[j].suit[0]) < parseInt(this.arrCard[i].suit[0])){
						var temp = this.arrCard[i];
						this.arrCard[i] = this.arrCard[j];
						this.arrCard[j] = temp;
					}
					if(parseInt(this.arrCard[j].suit[0]) == parseInt(this.arrCard[i].suit[0])){
						if(parseInt(this.arrCard[j].num[0]) < parseInt(this.arrCard[i].num[0])){
							var temp = this.arrCard[i];
							this.arrCard[i] = this.arrCard[j];
							this.arrCard[j] = temp;
						}
					}
				}
			}
			this.flagSort = true;
		}
	};
	//	dat quan bai nam trong fom ngang hoac doc(true:trong fom)
	this.setCheckH = function(_id, _flag) {
		this.arrCard[_id].checkH = _flag;
	};
	this.setCheckV = function(_id, _flag) {
		this.arrCard[_id].checkV = _flag;
	};
	//	khi an thi setTake quan bai an = true
	this.setTake = function(_take) {
		this.arrCard[_take].take = true;
	};
	this.findTake = function() {
		var count = 0;
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].take == true){
				count++;
			}
		}
		return count;
	};
	
	//khoi tao mang card down
	this.initCardDown = function(_state, _arrCard) {
		this.state = _state;
		for(var i=0;i<_arrCard.length;i++){
			this.arrCardDown.push(_arrCard[i]);
		}
	};
	this.addCardDown = function(_objCard) {
		this.arrCardDown.push(_objCard);
	};
	this.removeCardDown = function(_index){
		this.arrCardDown.splice(_index,1);
	};
	this.findTakeDown = function() {
		var count = 0;
		for(var i=0;i<this.arrCardDown.length;i++){
			if(this.arrCardDown[i].take == true){
				count++;
			}
		}
		return count;
	};
	this.findH = function() {
		var count = 0;
		for(var i=0;i<this.arrCardDown.length;i++){
			if(this.arrCardDown[i].checkH == true){
				count++;
			}
		}
		return count;
	};
	this.findV = function() {
		var count = 0;
		for(var i=0;i<this.arrCardDown.length;i++){
			if(this.arrCardDown[i].checkV == true){
				count++;
			}
		}
		return count;
	};
	this.totalPoint = function(){
		var point = 0;
		for(var i=0;i<this.arrCardDown.length;i++){
			point = point + this.arrCardDown[i].num[0];
		}
		return point;
	};
	
	return true;
}

function objTable(_id,_name,_markUnit){
	this.id= _id;
	this.name = _name;
	this.markUnit = _markUnit;
	this.numPlayer = 0;
	this.arrPlayer = [];	
	this.increaseNum = function() {
		this.numPlayer++;
	};
	this.decreaseNum = function() {
		this.numPlayer--;
	};
	this.addPlayer = function(_objPlayer) {
		this.arrPlayer.push(_objPlayer);
		this.increaseNum();
	};
	this.removePlayer = function(_index) {
		this.arrPlayer.splice(_index, 1);
		this.decreaseNum();
	};
	return true;
}


var objCardMe = new Object;
function initCardMe(_arrNumId, _arrSuitId){
	this.arrCard = [];
	for(var m=0;m<_arrNumId.length;m++){
		this.arrCard[m] = new objCard(_arrNumId[m], _arrSuitId[m]);
	}
	this.setClick = function(_id, _option){
		this.arrCard[_id].click = _option;
	};
	this.methodAdd = function(_objCard){
		this.arrCard.push(_objCard);
	};
	this.methodRemove = function(_index){
		this.arrCard.splice(_index,1);
	};
	// true[false] : sap xep theo so[chat]
	this.flagSort = null;
	this.sort = function(_flagSort){
		if(_flagSort == true){
			for(var i=0; i<this.arrCard.length; i++){
				for(var j=i+1;j<this.arrCard.length;j++){
					if(parseInt(this.arrCard[j].num[0]) < parseInt(this.arrCard[i].num[0])){
						var temp = this.arrCard[i];
						this.arrCard[i] = this.arrCard[j];
						this.arrCard[j] = temp;
					}
					if(parseInt(this.arrCard[j].num[0]) == parseInt(this.arrCard[i].num[0])){
						if(parseInt(this.arrCard[j].suit[0]) < parseInt(this.arrCard[i].suit[0])){
							var temp = this.arrCard[i];
							this.arrCard[i] = this.arrCard[j];
							this.arrCard[j] = temp;
						}
					}
				}
			}
			this.flagSort = false;
		}else{
			for(var i=0; i<this.arrCard.length; i++){
				for(var j=i+1;j<this.arrCard.length;j++){
					if(parseInt(this.arrCard[j].suit[0]) < parseInt(this.arrCard[i].suit[0])){
						var temp = this.arrCard[i];
						this.arrCard[i] = this.arrCard[j];
						this.arrCard[j] = temp;
					}
					if(parseInt(this.arrCard[j].suit[0]) == parseInt(this.arrCard[i].suit[0])){
						if(parseInt(this.arrCard[j].num[0]) < parseInt(this.arrCard[i].num[0])){
							var temp = this.arrCard[i];
							this.arrCard[i] = this.arrCard[j];
							this.arrCard[j] = temp;
						}
					}
				}
			}
			this.flagSort = true;
		}
	};
	//	dat quan bai nam trong fom ngang hoac doc(true:trong fom)
	this.setCheckH = function(_id, _flag) {
		this.arrCard[_id].checkH = _flag;
	};
	this.setCheckV = function(_id, _flag) {
		this.arrCard[_id].checkV = _flag;
	};
	//	khi an thi setTake quan bai an = true
	this.setTake = function(_take) {
		this.arrCard[_take].take = true;
	};
	this.findTake = function() {
		var count = 0;
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].take == true){
				count++;
			}
		}
		return count;
	};

	return true;
}

function objCardState(_state, _arrCard){//obj card nam trong fom
	this.state = _state;
	this.arrCard = [];
	for(var i=0;i<_arrCard.length;i++){
		this.arrCard.push(_arrCard[i]);
	}
	this.methodAdd = function(_objCard) {
		this.arrCard.push(_objCard);
	};
	this.methodRemove = function(_index) {
		this.arrCard.splice(_index,1);
	};
	this.findTake = function() {
		var count = 0;
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].take == true){
				count++;
			}
		}
		return count;
	};
	this.findH = function() {
		var count = 0;
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].checkH == true){
				count++;
			}
		}
		return count;
	};
	this.findV = function() {
		var count = 0;
		for(var i=0;i<this.arrCard.length;i++){
			if(this.arrCard[i].checkV == true){
				count++;
			}
		}
		return count;
	};
	this.totalPoint = function(){
		var point = 0;
		for(var i=0;i<this.arrCard.length;i++){
			point = point + this.arrCard[i].num[0];
		}
		return point;
	};
	return true;
}


/*************************** PLAYER VAO BAN *******************************/
//_position:true->bot, false->top.
//_sameIP:true->trung`, false:ko trung`
function loadDataBT(_position,_sameIP,_objPlayer){
	var idDiv;
	var idTd1;
	var idTd2;
	var idTd3;
	var classTd3;
	if(_position==true){
		idDiv='infoSeatBot';
		idTd1='seatBN';
		idTd2='seatBM';
		idTd3='seatBIP';
		if(_sameIP==true) classTd3='infoSameIP';
		else classTd3='infoIP';
	}else{
		idDiv='infoSeatTop';
		idTd1='seatTN';
		idTd2='seatTM';
		idTd3='seatTIP';
		if(_sameIP==true) classTd3='infoSameIP';
		else classTd3='infoIP';
	}
	
	var div = document.createElement('div');
	div.setAttribute('id',idDiv);
	div.setAttribute('class','tblSeat');
	document.getElementById("tblGame").appendChild(div);
	var tbl = document.createElement('table');
	tbl.setAttribute('class','tblInfo');
	tbl.setAttribute('border','0');
	tbl.setAttribute('cellpadding','0');
	tbl.setAttribute('cellspacing','0');
	div.appendChild(tbl);
	var tr = document.createElement('tr');
	
	var td = document.createElement('td');
	td.setAttribute('id',idTd1);
	td.setAttribute('class','infoName');
	var txt = document.createTextNode(_objPlayer.name);
	td.appendChild(txt);
	tr.appendChild(td);
	
	td = document.createElement('td');
	td.setAttribute('id',idTd2);
	td.setAttribute('class','infoMoney');
	txt = document.createTextNode(_objPlayer.mark);
	td.appendChild(txt);
	tr.appendChild(td);
	
	td = document.createElement('td');
	td.setAttribute('id',idTd3);
	td.setAttribute('class',classTd3);
	txt = document.createTextNode(_objPlayer.ip);
	td.appendChild(txt);
	tr.appendChild(td);
	
	tbl.appendChild(tr);
}

//_position:true->right, false->left
//_sameIP:true->trung`, false:ko trung`
function loadDataRL(_position,_sameIP,_objPlayer){
	var idDiv;
	var idLi1;
	var idLi2;
	var idLi3;
	var classLi3;
	if(_position==true){
		idDiv='infoSeatRight';
		idLi1='seatRN';
		idLi2='seatRM';
		idLi3='seatRIP';
		if(_sameIP==true) classLi3='infoSameIP';
		else classLi3='infoIP';
	}else{
		idDiv='infoSeatLeft';
		idLi1='seatLN';
		idLi2='seatLM';
		idLi3='seatLIP';
		if(_sameIP==true) classLi3='infoSameIP';
		else classLi3='infoIP';
	}
	var div = document.createElement('div');
	div.setAttribute('id',idDiv);
	div.setAttribute('class','infoSeat');
	document.getElementById("tblGame").appendChild(div);
	var ul = document.createElement('ul');
	ul.setAttribute('class','tblInfo');
	
	var li = document.createElement('li');
	li.setAttribute('id',idLi1);
	li.setAttribute('class','infoName');
	var txt = document.createTextNode(_objPlayer.name);
	li.appendChild(txt);
	ul.appendChild(li);
	
	li = document.createElement('li');
	li.setAttribute('id',idLi2);
	li.setAttribute('class','infoMoney');
	txt = document.createTextNode(_objPlayer.mark);
	li.appendChild(txt);
	ul.appendChild(li);
	
	li = document.createElement('li');
	li.setAttribute('id',idLi3);
	li.setAttribute('class',classLi3);
	txt = document.createTextNode(_objPlayer.ip);
	li.appendChild(txt);
	ul.appendChild(li);
	
	div.appendChild(ul);
}



/*************************** PLAY GAME ******************************/
function resetGame(){
	var ele = document.getElementById('table');
	while(ele.hasChildNodes()){
		ele.removeChild(ele.lastChild);
	}
}

function start(strStart){
	// Lay du lieu tu server: 
	document.getElementById('btnStart').style.display='none';
	document.getElementById('cardAll').style.display='block';
	document.getElementById('cardBot').style.display='block';
	document.getElementById('cardLeft').style.display='block';
	document.getElementById('cardTop').style.display='block';
	document.getElementById('cardRight').style.display='block';
	var ele = getElementsByClassName('gamble',document.getElementById('table'));
	for(var i=0;i<ele.length;i++){
		ele[i].style.display = 'block';
	}
	
	var arr = jsonStart(strStart);
	objCardMe = new initCardMe(arr[3], arr[4]);
	objCardMe.setTake(0);
	objCardMe.setTake(7);
//	objCardMe.setTake(6);
	for(var i=0;i<objCardMe.arrCard.length;i++){
		var j = i+1;
		var div = document.createElement('div');
		div.setAttribute('id','cardBot_'+j+'');
		div.setAttribute('class','card');
		div.setAttribute('onclick','clickCard(this)');
		div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
		document.getElementById("cardBot").appendChild(div);		
		var span = document.createElement('span');
		span.setAttribute('id','span'+j+'');
		span.setAttribute('class','spanNum');
		span.style.backgroundColor = '#fff';
		span.style.color = objCardMe.arrCard[i].suit[2];
		document.getElementById('cardBot_'+j+'').appendChild(span);
		var txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
		document.getElementById('span'+j+'').appendChild(txt);	
		var left = 9.5*i;
		new Motion(div,[50,-225],[left,0],[0,0],[12.5,100],150+i*20);
		div.style.display = 'block';
		div.style.zIndex = ''+j+'';
	}	
	new Motion(document.getElementById("cardLeft"),[50,50],[80,40],[0,0],[7,20],155);
	new Motion(document.getElementById("cardTop"),[50,50],[46,2],[0,0],[7,20],160);
	new Motion(document.getElementById("cardRight"),[50,50],[10.5,40],[0,0],[7,20],165);
//	startTimer("timerBot",1);
}

function sortCard(){
	if(objCardMe.flagSort == null) objCardMe.sort(false);
	else objCardMe.sort(objCardMe.flagSort);
	for(var i=0;i<objCardMe.arrCard.length;i++){
		var j = i+1;
		removeElement('cardBot','cardBot_'+j+'');
		var span = document.createElement('span');
		span.setAttribute('id','span'+j+'');
		span.setAttribute('class','spanNum');
		span.style.backgroundColor = '#fff';
		span.style.color = objCardMe.arrCard[i].suit[2];
		var _txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
		span.appendChild(_txt);
		
		var div = document.createElement('div');
		document.getElementById("cardBot").appendChild(div);
		div.setAttribute('id','cardBot_'+j+'');
		if(objCardMe.arrCard[i].take == true){
			div.setAttribute('class','cardTake');
			var _div = document.createElement('div');
			_div.setAttribute('class','card');
			_div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
			_div.appendChild(span);
			_div.style.display = 'block';
			div.appendChild(_div);
			new Motion(div,[0,0],[9.5*i,-6],[12.5,100],[14,108],20+i*5);
		}else{
			div.setAttribute('class','card');
			div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
			div.appendChild(span);
			new Motion(div,[0,0],[9.5*i,0],[12.5,100],[12.5,100],20+i*5);
		}
		div.setAttribute('onclick','clickCard(this)');
		div.style.display = 'block';
		div.style.zIndex = ''+j+'';
	}
}

/******************** DANH BAI ***********************/
function clickCard(_obj){
	var _objid = _obj.id;
	var index = _objid.indexOf('_');
	var count = parseInt(_objid.substr(index+1,_objid.length))-1;
	if(objCardMe.arrCard[count].click == 0){
		if(objCardMe.arrCard[count].take == true){
			_obj.style.top="-26%";
		}else _obj.style.top="-20%";
		objCardMe.setClick(count, 1);
		return;
	}else{
		if(objCardMe.arrCard[count].take == true){
			_obj.style.top="-6%";
		}else _obj.style.top="0%";
		objCardMe.setClick(count, 0);
		return;
	}
}

// Function lay bai danh cua cac gamer # tu server va hien thi
function getGamble(_idGamer, _objCard){
	for(var i=0;i<arrCardGamble.length;i++){
		if(arrCardGamble[i].idGamer == _idGamer){
			var _id = _idGamer+1;
			arrCardGamble[i].methodAdd(_objCard);			
			var _round = round+1;
			
			var div = document.createElement('div');
			div.setAttribute('id','gamble'+_id+'_'+_round+'');
			div.setAttribute('class','card_gamble');
			div.style.backgroundImage='url('+arrCardGamble[i].arrCard[round].suit[1]+')';
			document.getElementById('gamer'+_id+'_gamble').appendChild(div);
			
			var span = document.createElement('span');
			span.setAttribute('id','spanGamble'+_id+'_'+_round+'');
			span.setAttribute('class','spanNum');
			document.getElementById('gamble'+_id+'_'+_round+'').appendChild(span);
			var txt = document.createTextNode(arrCardGamble[i].arrCard[round].num[1]);		
			document.getElementById('spanGamble'+_id+'_'+_round+'').appendChild(txt);		
			span.style.backgroundColor = '#fff';
			span.style.color = arrCardGamble[i].arrCard[round].suit[2];		
			if(_idGamer == 1){
				new Motion(div,[115,105],[20.5*round,0],[35,100],[35,100],15);
			}else if(_idGamer == 2){
				new Motion(div,[-70,125],[20.5*round,0],[35,100],[35,100],15);
			}else if(_idGamer ==3){
				new Motion(div,[-80,-53],[20.5*round,0],[35,100],[35,100],15);
			}
			div.style.display ='block';
			div.style.zIndex = ''+_round+'';			
			break;
		}
	}	
}

// Function đánh bài cua Me
function setGamble(){
	if(round>3) return;
	var count = 0;
	var index=null;	
	for(var i=0;i<objCardMe.arrCard.length;i++){
		if(objCardMe.arrCard[i].click == 1){
			count++;
			index = i;
			if(count > 1) break;
		}
	}
	if(count == 0 || count >1) return;
	else{
		objCardMe.setClick(index,0);
		var _round = round+1;
		
		var div = document.createElement('div');
		div.setAttribute('id','gambleBot_'+_round+'');
		div.setAttribute('class','card_gamble');
		div.style.backgroundImage='url('+objCardMe.arrCard[index].suit[1]+')';
		document.getElementById("gamerBot_gamble").appendChild(div);
		
		var span = document.createElement('span');
		span.setAttribute('id','spanGambleB_'+_round+'');
		span.setAttribute('class','spanNum');
		document.getElementById('gambleBot_'+_round+'').appendChild(span);
		var txt = document.createTextNode(objCardMe.arrCard[index].num[1]);		
		document.getElementById('spanGambleB_'+_round+'').appendChild(txt);		
		span.style.backgroundColor = '#fff';
		span.style.color = objCardMe.arrCard[index].suit[2];		
		
		new Motion(div,[-50,130],[20.5*round,0],[35,100],[35,100],15);
		div.style.display ='block';
		div.style.zIndex = ''+_round+'';
		// Sau khi co function boc' hoac an thi round se duoc tang o day
		round++;
		
		removeElement('cardBot','cardBot_'+objCardMe.arrCard.length+'');
		// arrCardGamble[0].methodAdd(objCardMe.arrCard[index]);
		objCardMe.methodRemove(index);
		for(var i=index;i<objCardMe.arrCard.length;i++){
			var j = i+1;
			removeElement('cardBot','cardBot_'+j+'');
			var span = document.createElement('span');
			span.setAttribute('id','span'+j+'');
			span.setAttribute('class','spanNum');
			span.style.backgroundColor = '#fff';
			span.style.color = objCardMe.arrCard[i].suit[2];
			var _txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
			span.appendChild(_txt);
			
			var div = document.createElement('div');
			document.getElementById("cardBot").appendChild(div);
			div.setAttribute('id','cardBot_'+j+'');
			if(objCardMe.arrCard[i].take == true){
				div.setAttribute('class','cardTake');
				var _div = document.createElement('div');
				_div.setAttribute('class','card');
				_div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
				_div.appendChild(span);
				_div.style.display = 'block';
				div.appendChild(_div);
				new Motion(div,[0,0],[9.5*i,-6],[12.5,100],[14,108],5+i*5);
			}else{
				div.setAttribute('class','card');
				div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
				div.appendChild(span);
				new Motion(div,[0,0],[9.5*i,0],[12.5,100],[12.5,100],5+i*5);
			}
			div.setAttribute('onclick','clickCard(this)');
			div.style.display = 'block';
			div.style.zIndex = ''+j+'';
		}
	}
}

function autoGamble(){
	var arrCardSV = checkStateVer();
	var arrCardSH = checkStateHor();
	var rdCard=0;
	while(1){
		var flag=false;
		rdCard = Math.floor(Math.random()*(objCardMe.arrCard.length-1));
		var cardGamble = objCardMe.arrCard[rdCard];
		for(var i=0;i<arrCardSH.length;i++){
			if(arrCardSH[i].findTake()>0){
				for(var j=0;j<arrCardSH[i].arrCard.length;j++){
					if(cardGamble == arrCardSH[i].arrCard[j]){
						flag=true;break;
					}
				}
			}
		}
		for(var i=0;i<arrCardSV.length;i++){
			if(arrCardSV[i].findTake()>0){
				for(var j=0;j<arrCardSV[i].arrCard.length;j++){
					if(cardGamble == arrCardSV[i].arrCard[j]){
						flag=true;break;
					}
				}
			}
		}
		if(flag==false) break;
	}
	
	var _round = round+1;
	
	var div = document.createElement('div');
	div.setAttribute('id','gambleBot_'+_round+'');
	div.setAttribute('class','card_gamble');
	div.style.backgroundImage='url('+objCardMe.arrCard[rdCard].suit[1]+')';
	document.getElementById("gamerBot_gamble").appendChild(div);
	
	var span = document.createElement('span');
	span.setAttribute('id','spanGambleB_'+_round+'');
	span.setAttribute('class','spanNum');
	document.getElementById('gambleBot_'+_round+'').appendChild(span);
	var txt = document.createTextNode(objCardMe.arrCard[rdCard].num[1]);		
	document.getElementById('spanGambleB_'+_round+'').appendChild(txt);		
	span.style.backgroundColor = '#fff';
	span.style.color = objCardMe.arrCard[rdCard].suit[2];		
	
	new Motion(div,[-50,130],[20.5*round,0],[35,100],[35,100],15);
	div.style.display ='block';
	div.style.zIndex = ''+_round+'';
	// Sau khi co function boc' hoac an thi round se duoc tang o day
	round++;
	
	removeElement('cardBot','cardBot_'+objCardMe.arrCard.length+'');
	// arrCardGamble[0].methodAdd(objCardMe.arrCard[rdCard]);
	objCardMe.methodRemove(rdCard);
	for(var i=rdCard;i<objCardMe.arrCard.length;i++){
		var j = i+1;
		removeElement('cardBot','cardBot_'+j+'');
		var span = document.createElement('span');
		span.setAttribute('id','span'+j+'');
		span.setAttribute('class','spanNum');
		span.style.backgroundColor = '#fff';
		span.style.color = objCardMe.arrCard[i].suit[2];
		var _txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
		span.appendChild(_txt);
		
		var div = document.createElement('div');
		document.getElementById("cardBot").appendChild(div);
		div.setAttribute('id','cardBot_'+j+'');
		if(objCardMe.arrCard[i].take == true){
			div.setAttribute('class','cardTake');
			var _div = document.createElement('div');
			_div.setAttribute('class','card');
			_div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
			_div.appendChild(span);
			_div.style.display = 'block';
			div.appendChild(_div);
			new Motion(div,[0,0],[9.5*i,-6],[12.5,100],[14,108],5+i*5);
		}else{
			div.setAttribute('class','card');
			div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
			div.appendChild(span);
			new Motion(div,[0,0],[9.5*i,0],[12.5,100],[12.5,100],5+i*5);
		}
		div.setAttribute('onclick','clickCard(this)');
		div.style.display = 'block';
		div.style.zIndex = ''+j+'';
	}	
}

// Function chuyển hạ
function changeGamble(){

}


/******************** HA BAI ***********************/
var arrCardState = new Array();
//Kiem tra so fom ngang co the co, return mang fom ngang
function checkStateHor(){
	var _arrCardSH = new Array();
	var state = 0;
	for(var i=0;i<objCardMe.arrCard.length-2;i++){
		if(objCardMe.arrCard[i].checkH == true) continue;
		for(var j=i+1;j<objCardMe.arrCard.length-1;j++){
			if(objCardMe.arrCard[j].checkH == true) continue;
			for(var k=j+1;k<objCardMe.arrCard.length;k++){
				if(objCardMe.arrCard[k].checkH == true) continue;
				if(state==3) break;
				if(_arrCardSH.length==3) break;
				if(parseInt(objCardMe.arrCard[j].num[0]) == parseInt(objCardMe.arrCard[i].num[0]) &&
						parseInt(objCardMe.arrCard[k].num[0]) == parseInt(objCardMe.arrCard[j].num[0])){
					state++;
					objCardMe.setCheckH(i, true);objCardMe.setCheckH(j, true);objCardMe.setCheckH(k, true);
					_arrCardSH.push(new objCardState(state, [objCardMe.arrCard[i], objCardMe.arrCard[j], objCardMe.arrCard[k]]));
					for(var l=k+1;l<objCardMe.arrCard.length;l++){
						if(parseInt(objCardMe.arrCard[l].num[0]) == parseInt(objCardMe.arrCard[k].num[0])){
							for(var m=0;m<_arrCardSH.length;m++){
								if(_arrCardSH[m].state == state){
									objCardMe.setCheckH(l, true);
									_arrCardSH[m].methodAdd(objCardMe.arrCard[l]);
									break;
								}
							}
						}
					}
				}
			}			
		}
	}
	return _arrCardSH;
}
//Kiem tra so fom doc co the co, return mang fom doc
function checkStateVer(){
	if(objCardMe.flagSort == null) sortCard();
	var _arrCardSV = new Array();
	var state = 0;
	for(var i=0;i<objCardMe.arrCard.length-2;i++){
		if(objCardMe.arrCard[i].checkV == true) continue;
		for(var j=i+1;j<objCardMe.arrCard.length-1;j++){
			if(objCardMe.arrCard[j].checkV == true) continue;
			for(var k=j+1;k<objCardMe.arrCard.length;k++){
				if(objCardMe.arrCard[k].checkV == true) continue;
				if(state==3) break;
				if(_arrCardSV.length==3) break;
				if(parseInt(objCardMe.arrCard[j].suit[0]) == parseInt(objCardMe.arrCard[i].suit[0]) &&
						parseInt(objCardMe.arrCard[k].suit[0]) == parseInt(objCardMe.arrCard[j].suit[0])){
					var a = objCardMe.arrCard[i].num[0];
					var b = objCardMe.arrCard[j].num[0];
					var c = objCardMe.arrCard[k].num[0];
					if(a==b-1 && b==c-1){
						state++;
						objCardMe.setCheckV(i, true);objCardMe.setCheckV(j, true);objCardMe.setCheckV(k, true);
						_arrCardSV.push(new objCardState(state, [objCardMe.arrCard[i], objCardMe.arrCard[j], objCardMe.arrCard[k]]));
						for(var l=k+1;l<objCardMe.arrCard.length;l++){
							if(parseInt(objCardMe.arrCard[l].suit[0]) == parseInt(objCardMe.arrCard[k].suit[0])){
								if(c==objCardMe.arrCard[l].num[0]-1 || a == objCardMe.arrCard[l].num[0]+1){
									for(var m=0;m<_arrCardSV.length;m++){
										if(_arrCardSV[m].state == state){
											objCardMe.setCheckV(l, true);
											if(c==objCardMe.arrCard[l].num[0]-1){
												c = objCardMe.arrCard[l].num[0];
												_arrCardSV[m].methodAdd(objCardMe.arrCard[l]);
											}else{
												a = objCardMe.arrCard[l].num[0];
												_arrCardSV[m].arrCard.splice(0,0,objCardMe.arrCard[l]);
											}
											//Check xem fom doc co chia dc lam 2,3 fom rieng ko
											if(_arrCardSV[m].arrCard.length >=6){
												var arrTemp = _arrCardSV[m].arrCard.splice(3,_arrCardSV[m].arrCard.length-1);
												state++;
												_arrCardSV.push(new objCardState(state, arrTemp));
												m++;
											}
											break;
										}
									}
								}
							}
						}
					}
				}
			}			
		}		
	}
	return _arrCardSV;
}

// kiem tra xem fom nao dc uu tien:(cay an>diem)
function priorityFom(fom1, fom2){
	if(fom1.findTake()>0 && fom2.findTake()==0) return 1;
	else if(fom1.findTake()==0 && fom2.findTake()>0) return 2;
	else if(fom1.findTake()>0 && fom2.findTake()>0){
		if(fom1.totalPoint()>=fom2.totalPoint()) return 3;
		else if(fom1.totalPoint()<fom2.totalPoint()) return 4;
	}else{
		if(fom1.totalPoint()>fom2.totalPoint()) return 1;
		else if(fom1.totalPoint()<fom2.totalPoint()) return 2;
		else if(fom1.totalPoint()==fom2.totalPoint()) return 0;
	}
}

// Remove Card cua fom doc trong truong hop(4 6 7 8)--posCard: card 5
function removeCardSV(fom, posCard){
	if(fom.arrCard.length==3) return;
	for(var k=0;k<fom.arrCard.length-1;k++){
		if(fom.arrCard[k+1].num[0]-fom.arrCard[k].num[0]!=1){
			if(posCard<fom.arrCard.length/2){
				fom.methodRemove(k);
			}else fom.methodRemove(k+1);
			if(k==0) k=0;
			else k--;
		}
	}
}

// quan bai fom ngang nam o dau hoac cuoi fom doc-->tach duoc--> return true
// ap dung cho fom doc co > 4con
function checkRemoveCardSV(_numCard, _stateLength){
	if(_stateLength==3) return false;
	if(_stateLength>5) return true;
	var x=Math.floor((_stateLength-1) / 2);
	var y=(_stateLength-1) % 2;
	if(y==0){
		if(_numCard>x || _numCard<x) return true;
		else return false;
	}else{
		if(_numCard>x+y || _numCard<x) return true;
		else return false;
	}
}

function clickDownCard(){
	displayPopup();
//	manualSetState();
}

function autoDown(){
	var radioValue=null;
	for (var i=0;i<document.getElementsByName('state').length;i++) {
		if (document.getElementsByName('state')[i].checked) {
			radioValue = document.getElementsByName('state')[i].value;
			break;
		}
	}
	closePopUp();
	if(radioValue=='auto'){
		var check = autoDownState();
		if(check == -1){
			alert("Ăn láo!!!");
		}else if(check == 0){
			alert("Móm!!");
		}else if(check >0){
			displayDownCard();
			if(check==10) alert("Ù.....");
			else if(check==100) alert("Ù đền.......");
		}
	}
}

// Function auto hạ bài cua Me
function autoDownState(){
	objCardState.prototype.priority = 0;
	for(var i=0;i<objCardMe.arrCard.length;i++){
		objCardMe.setCheckH(i, false);
		objCardMe.setCheckV(i, false);
	}
	var arrCardSV = checkStateVer();
	var arrCardSH = checkStateHor();
	
//	tempH/tempV: [][][]: [0]-fom i cua arrCardSH/arrCardSV, [1]-quan bai, [2]-card j trong fom i
	var tempH = new Array();
	var tempV = new Array();
//	tim nhung quan bai vua xep dc fom doc lan fom ngang trong arrCardSH/arrCardSV, luu vao tempH/tempV
//	neu fom ko co quan bai nhu vay thi set priority=1
	var state=0;
	for(var i=0;i<arrCardSH.length;i++){
		for(var j=0;j<arrCardSH[i].arrCard.length;j++){
			if(arrCardSH[i].arrCard[j].checkH == true && arrCardSH[i].arrCard[j].checkV == true){
				tempH.push([i, arrCardSH[i].arrCard[j], j]);
				state=arrCardSH[i].state;
			}
		}
		if(arrCardSH[i].state!=state) arrCardSH[i].priority = 1;
	}
	state=0;
	for(var i=0;i<arrCardSV.length;i++){
		for(var j=0;j<arrCardSV[i].arrCard.length;j++){
			if(arrCardSV[i].arrCard[j].checkH == true && arrCardSV[i].arrCard[j].checkV == true){
				tempV.push([i, arrCardSV[i].arrCard[j], j]);
				state=arrCardSV[i].state;
			}
		}
		if(arrCardSV[i].state!=state) arrCardSV[i].priority = 1;
	}
	
	var tempFom = new Array();
//[0]:quan bai,[1]:fom ngang thu i,[2]:card j ngang i,[3]:fom doc thu k,[4]:card m fom doc k
	for(var i=0;i<tempH.length;i++){
		for(var j=0;j<tempV.length;j++){
			if(tempH[i][1] == tempV[j][1]){
				tempFom.push([tempH[i][1], tempH[i][0], tempH[i][2], tempV[j][0], tempV[j][2]]);
				break;
			}
		}
	}	
	
//	chia lai fom theo tempFom:nhung con card trung
	for(var i=0;i<tempFom.length;i++){
		//fom ngang co >=2 con xep duoc fom doc, fom doc co 1 con xep fom ngang 
		if(arrCardSH[tempFom[i][1]].findV()>1 && arrCardSV[tempFom[i][3]].findH()==1){
			//fom ngang 0 an
			if(arrCardSH[tempFom[i][1]].findTake()==0){			
				for(var j=i;j<tempFom.length;j++){
					if(tempFom[j][1] == tempFom[i][1]){
						//	fom doc co 3 cay hoac card trung ko nam trong vi tri co the tach duoc
						if(checkRemoveCardSV(tempFom[j][4], arrCardSV[tempFom[j][3]].arrCard.length)==false){
							arrCardSV[tempFom[j][3]].priority = 1;
							arrCardSH[tempFom[i][1]].methodRemove(tempFom[j][2]);
							if(arrCardSH[tempFom[i][1]].arrCard.length<3)
								arrCardSH[tempFom[i][1]].priority = 0;
							else arrCardSH[tempFom[i][1]].priority = 1;
						}else{
						// fom doc > 3 cay, fom ngang = 4cay va card trung tach duoc
							arrCardSV[tempFom[j][3]].methodRemove(tempFom[j][4]);
							removeCardSV(arrCardSV[tempFom[j][3]], tempFom[j][4]);
							arrCardSV[tempFom[j][3]].priority = 1;
							arrCardSH[tempFom[i][1]].priority = 1;
						}
					}
				}
				
			//fom ngang co 1 con an
			}else if(arrCardSH[tempFom[i][1]].findTake()==1){
				var check = false;// kt xem an vao card trung--> true
				for(var j=i;j<tempFom.length;j++){
					if(tempFom[j][1]==tempFom[i][1] && tempFom[j][0].take==true){
						check = true;
						break;
					}
				}
				// fom ngang 3 card
				if(arrCardSH[tempFom[i][1]].arrCard.length==3){
					// an vao con vua xep doc vua xep ngang-->uu tien ha het fom doc
					if(check==true){
						for(var j=0;j<tempFom.length;j++){
							if(tempFom[j][1] == tempFom[i][1]){
								arrCardSV[tempFom[j][3]].priority = 1;
							}
						}
						arrCardSH[tempFom[i][1]].priority = 0;
					}else{// ko an vao con vua xep doc va ngang--> ha ngang, 
						  // xet cac fom doc co card trung trong fom ngang-->thoa man checkRemoveCardSV() 
						  // thi tach card va ha doc
						arrCardSH[tempFom[i][1]].priority = 1;
						for(var j=0;j<tempFom.length;j++){
							if(tempFom[j][1] == tempFom[i][1]){
								if(checkRemoveCardSV(tempFom[j][4], arrCardSV[tempFom[j][3]].arrCard.length)==true){
									arrCardSV[tempFom[j][3]].methodRemove(tempFom[j][4]);
									removeCardSV(arrCardSV[tempFom[j][3]], tempFom[j][4]);
									arrCardSV[tempFom[j][3]].priority = 1;
								}else arrCardSV[tempFom[j][3]].priority = 0;
							}
						}
					}
				// fom ngang 4 card
				}else if(arrCardSH[tempFom[i][1]].arrCard.length==4){
					// fom doc thoa man checkRemoveCardSV()
					if(checkRemoveCardSV(tempFom[i][4], arrCardSV[tempFom[i][3]].arrCard.length)==true){
						//TH: (45678), (5678) 5 la card trung
						arrCardSV[tempFom[i][3]].methodRemove(tempFom[i][4]);
						removeCardSV(arrCardSV[tempFom[i][3]], tempFom[i][4]);
						arrCardSV[tempFom[i][3]].priority = 1;
						var select = i+1;
						for(var j=i+1;j<tempFom.length;j++){
							for(var k=j+1;k<tempFom.length;k++){
								if(tempFom[j][1] == tempFom[k][1]){
									var choice = priorityFom(arrCardSV[tempFom[j][3]], arrCardSV[tempFom[k][3]]);
									if(choice<=1) select=j;
									else if(choice==2) select=k;
									else if(choice>=3) return -1;
								}
							}
						}
						arrCardSV[tempFom[select][3]].priority = 1;
						arrCardSH[tempFom[i][1]].methodRemove(tempFom[select][2]);
						arrCardSH[tempFom[i][1]].priority = 1;
					}else{
						//TH: (567),(345),(4567) voi 5 la card trung-->chon fom doc theo priorityFom()
						if(check==true){// an vao card trung
							// remove card trung trong ngang va ha doc ha ngang.
								arrCardSH[tempFom[i][1]].methodRemove(tempFom[i][2]);
								arrCardSV[tempFom[i][3]].priority = 1;
								for(var j=i+1;j<tempFom.length;j++){
									if(tempFom[j][1] == tempFom[i][1]){
										var choice = priorityFom(arrCardSH[tempFom[i][1]], arrCardSV[tempFom[j][3]]);
										if(choice<=1) arrCardSH[tempFom[i][1]].priority = 1;
										else if(choice==2) arrCardSV[tempFom[j][3]].priority = 1;
										else if(choice>=3) return -1;
									}
								}
							}else{ // an vao card ko trung--> ha ngang 3 cay va it nhat 1 doc
								var select = i;
								for(var j=i;j<tempFom.length;j++){
									for(var k=j+1;k<tempFom.length;k++){
										if(tempFom[j][1] == tempFom[k][1]){
											var choice = priorityFom(arrCardSV[tempFom[j][3]], arrCardSV[tempFom[k][3]]);
											if(choice<=1) select=j;
											else if(choice==2) select=k;
											else if(choice>=3) return -1;
										}
									}
								}
								arrCardSV[tempFom[select][3]].priority = 1;
								arrCardSH[tempFom[i][1]].methodRemove(tempFom[select][2]);
								arrCardSH[tempFom[i][1]].priority = 1;
							}
					}
				}
			
			//fom ngang an > 1 con
			}else if(arrCardSH[tempFom[i][1]].findTake()>1){
				// fom ngang co 3 card--> ha het doc
				if(arrCardSH[tempFom[i][1]].arrCard.length==3){
					for(var j=0;j<tempFom.length;j++){
						if(tempFom[j][1] == tempFom[i][1]){
							arrCardSV[tempFom[j][3]].priority = 1;
						}
					}
					arrCardSH[tempFom[i][1]].priority = 0;
				// fom ngang co 4 card	
				}else if(arrCardSH[tempFom[i][1]].arrCard.length==4){
					// kiem tra xem cac con card an co vua nam doc vua nam ngang ko
					// check:dem' so, state:neu chi an 1 cay vua doc va ngang thi luu stt card day
					var check =0;var state=-1;
					for(var j=0;j<tempFom.length;j++){
						if(tempFom[j][1]==tempFom[i][1] && tempFom[j][0].take==true){
							check++;state=j;
						}
					}
					if(check>1){
						for(var j=0;j<tempFom.length;j++){
							if(tempFom[j][1] == tempFom[i][1]){
								//fom doc co card trung ko tach duoc checkRemoveCardSV()
								if(checkRemoveCardSV(tempFom[j][4], arrCardSV[tempFom[j][3]].arrCard.length)==false){
									arrCardSV[tempFom[j][3]].priority = 1;
									arrCardSH[tempFom[i][1]].methodRemove(tempFom[j][2]);
									arrCardSH[tempFom[i][1]].priority = 0;
								}else{
									arrCardSV[tempFom[j][3]].methodRemove(tempFom[j][4]);
									removeCardSV(arrCardSV[tempFom[j][3]], tempFom[j][4]);
									arrCardSV[tempFom[j][3]].priority = 1;
									arrCardSH[tempFom[i][1]].priority = 1;
								}
							}
						}
					}else if(check==1){
						arrCardSV[tempFom[state][3]].priority = 1;
						arrCardSH[tempFom[i][1]].methodRemove(tempFom[state][2]);
						for(var j=0;j<tempFom.length;j++){
							if(tempFom[j][1] == tempFom[i][1] && j!=state){
								//fom doc ko tach dc card trung checkRemoveCardSV()
								if(checkRemoveCardSV(tempFom[j][4], arrCardSV[tempFom[j][3]].arrCard.length)==false){
									arrCardSH[tempFom[i][1]].priority=1;
									arrCardSV[tempFom[j][3]].priority=0;
								}else{
									arrCardSV[tempFom[j][3]].methodRemove(tempFom[j][4]);
									removeCardSV(arrCardSV[tempFom[j][3]], tempFom[j][4]);
									arrCardSV[tempFom[j][3]].priority = 1;
									arrCardSH[tempFom[i][1]].priority = 1;
								}
							}
						}
					}else if(check ==0){
						return -1;
					}
				}
			}
			i=i+arrCardSH[tempFom[i][1]].findV();
			
		//fom doc co >=2 con xep duoc fom ngang, fom ngang co 1 con xep fom doc
		}else if(arrCardSH[tempFom[i][1]].findV()==1 && arrCardSV[tempFom[i][3]].findH()>1){
			var count=0;// dem so fom ngang 4 cay(max =2 fom)
			var sh4 = null;// luu vi tri fom ngang 4 cay trong truong hop co 0<count<2
			var sh3 = null;// luu vi tri fom ngang 3 cay trong truong hop co 0<count<2
			var option=false;// fom doc co 2 card trung ko tach duoc hoac co 3 card trung--> true
			var shRemove=null;// luu vi tri fom ngang co the xoa de lay fom doc
			var shDown=null;// luu vi tri fom ngang ha
			for(var j=i;j<tempFom.length;j++){
				if(tempFom[j][3] == tempFom[i][3]){
					if(arrCardSH[tempFom[j][1]].arrCard.length == 4){
						count++; sh4 = tempFom[j];
					}
					else sh3 = tempFom[j];
					if(j<tempFom.length-1){
						if(arrCardSV[tempFom[i][3]].findH()==3) option=true;
						if(checkRemoveCardSV(tempFom[j][4], arrCardSV[tempFom[j][3]].arrCard.length)==false &&
								checkRemoveCardSV(tempFom[j+1][4], arrCardSV[tempFom[j+1][3]].arrCard.length)==false){
							option=true;
						}else if(checkRemoveCardSV(tempFom[j][4], arrCardSV[tempFom[j][3]].arrCard.length)==true && arrCardSV[tempFom[i][3]].findH()==2){
							shRemove = tempFom[j+1];shDown=tempFom[j];
						}else if(checkRemoveCardSV(tempFom[j+1][4], arrCardSV[tempFom[j+1][3]].arrCard.length)==true  && arrCardSV[tempFom[i][3]].findH()==2){
							shRemove = tempFom[j];shDown=tempFom[j+1];
						}
					}
				}
			}
			//co 2 fom ngang 4 cay--> ha doc, ngang 3 cay(3 fom-> u` hoac lao')
			if(count==2){
				arrCardSV[tempFom[i][3]].priority = 1;
				for(var j=i;j<tempFom.length;j++){
					if(tempFom[j][3] == tempFom[i][3]){
						arrCardSH[tempFom[j][1]].methodRemove(tempFom[j][2]);
						arrCardSH[tempFom[j][1]].priority = 1;
					}
				}
				break;
			}
			
			//fom doc 0 an
			if(arrCardSV[tempFom[i][3]].findTake()==0){
				if(count==0){//ko co fom ngang 4
					// uu tien ha ngang
					if(option==true){
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][3] == tempFom[i][3]){
								arrCardSH[tempFom[j][1]].priority = 1;
							}
						}
						arrCardSV[tempFom[i][3]].priority = 0;
					}else{// xet uu tien fom ngang co the xoa va fom doc
						arrCardSH[shDown[1]].priority = 1;
						arrCardSV[tempFom[i][3]].methodRemove(shDown[4]);
						removeCardSV(arrCardSV[tempFom[i][3]], shDown[4]);
						var choice = priorityFom(arrCardSH[shRemove[1]], arrCardSV[tempFom[i][3]]);
						if(choice<=1) arrCardSH[shRemove[1]].priority = 1;
						else if(choice==2) arrCardSV[tempFom[i][3]].priority = 1;
					}
				}else{//co 1 fom ngang 4 va 1 fom ngang 3
					if(arrCardSH[sh4[1]].findTake()>1){//fom 4 an nhieu hon 1 cay
						return -1;
					}else{//xet diem cua fom doc neu ko co card sh4[0] va fom ngang 3
						arrCardSV[tempFom[i][3]].methodRemove(sh4[4]);
						if(arrCardSV[tempFom[i][3]].arrCard.length<3){
							arrCardSV[tempFom[i][3]].priority = 0;
							for(var j=i;j<tempFom.length;j++){
								if(tempFom[j][3] == tempFom[i][3]){
									arrCardSH[tempFom[j][1]].priority = 1;
								}
							}
						}else{
							var choice = priorityFom(arrCardSH[sh3[1]], arrCardSV[tempFom[i][3]]);
							if(choice<=1) arrCardSH[sh3[1]].priority = 1;
							else if(choice==2){
								arrCardSH[sh4[1]].methodRemove(sh4[2]);
								arrCardSV[tempFom[i][3]].arrCard.splice(sh4[4],0,sh4[0]);
								arrCardSV[tempFom[i][3]].priority = 1;
							}
							arrCardSH[sh4[1]].priority = 1;
						}
					}
				}
			}
			//fom doc an 1		
			else if(arrCardSV[tempFom[i][3]].findTake()==1){
				var check = false;// kt xem an vao card trung--> true
				for(var j=i;j<tempFom.length;j++){
					if(tempFom[j][3]==tempFom[i][3] && tempFom[j][0].take==true){
						check = true;
						break;
					}
				}
				if(count==0){//ko co fom ngang 4
					// ko an card trung, option=true -> fai ha doc
					if(check==false && option==true){
						arrCardSV[tempFom[i][3]].priority = 1;
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][3] == tempFom[i][3]){
								arrCardSH[tempFom[j][1]].priority = 0;
							}
						}
					}// an card trung, option=true -> ha ngang
					else if(check==true && option==true){
						arrCardSV[tempFom[i][3]].priority = 0;
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][3] == tempFom[i][3]){
								arrCardSH[tempFom[j][1]].priority = 1;
							}
						}
					}//option=false ->ha 1 fom ngang shDown va xet do uu tien cua 1 fom doc va ngang shRemove
					else if(option==false){
						arrCardSH[shDown[1]].priority = 1;
						arrCardSV[tempFom[i][3]].methodRemove(shDown[4]);
						removeCardSV(arrCardSV[tempFom[i][3]], shDown[4]);
						var choice = priorityFom(arrCardSH[shRemove[1]], arrCardSV[tempFom[i][3]]);
						if(choice<=1 || choice==3) arrCardSH[shRemove[1]].priority = 1;
						else if(choice==2 || choice==4) arrCardSV[tempFom[i][3]].priority = 1;
					}
				}else{//co 1 fom ngang 4 va 1 fom ngang 3
					if((check==false||arrCardSV[tempFom[i][3]].arrCard.length>3) && option==true){
						//an card 0 trung,doc ko tach duoc-->ha doc va fom ngang 4 ha 3
						//hoac doc>3 va ko tach duoc
						arrCardSV[tempFom[i][3]].priority = 1;
						arrCardSH[sh4[1]].methodRemove(sh4[2]);
						arrCardSH[sh4[1]].priority = 1;
					}else if(check==true && option==true){
						//an card trung,doc 0 tach duoc-> ha het ngang
						arrCardSV[tempFom[i][3]].priority = 0;
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][3] == tempFom[i][3]){
								arrCardSH[tempFom[j][1]].priority = 1;
							}
						}
					}else if(option==false){// fom doc tach duoc
						if((checkRemoveCardSV(sh4[4], arrCardSV[sh4[3]].arrCard.length)==true &&
								checkRemoveCardSV(sh3[4], arrCardSV[sh3[3]].arrCard.length)==true && sh4[4]>sh3[4]) ||
								arrCardSH[sh4[1]].findTake()>1){
							arrCardSV[tempFom[i][3]].methodRemove(sh3[4]);
							removeCardSV(arrCardSV[tempFom[i][3]], sh3[4]);
							arrCardSV[tempFom[i][3]].priority = 1;
							arrCardSH[sh4[1]].methodRemove(sh4[2]);
							arrCardSH[sh4[1]].priority = 1;
							arrCardSH[sh3[1]].priority = 1;
						}else{
							arrCardSV[tempFom[i][3]].priority = 1;
							arrCardSH[sh4[1]].methodRemove(sh4[2]);
							arrCardSH[sh4[1]].priority = 1;
						}
					}
				}
			}
			//fom doc an>1
			else if(arrCardSV[tempFom[i][3]].findTake()>1){
				// kiem tra xem cac con card an co vua nam doc vua nam ngang ko
				// check:dem' so, state:neu chi an 1 cay vua doc va ngang thi luu card day
				var check =0;var state=null;
				for(var j=0;j<tempFom.length;j++){
					if(tempFom[j][3]==tempFom[i][3] && tempFom[j][0].take==true){
						check++;state=tempFom[j];
					}
				}
				var z=-1;//stt card an ko fai la card trung
				for(var j=0;j<arrCardSV[tempFom[i][3]].arrCard.length;j++){
					if(arrCardSV[tempFom[i][3]].arrCard[j].take ==true && 
						!(arrCardSV[tempFom[i][3]].arrCard[j].checkV==true &&
						  arrCardSV[tempFom[i][3]].arrCard[j].checkH==true)){
						z=j;break;
					}
				}
				if(check==0 || arrCardSV[tempFom[i][3]].findTake()-check>1) return -1;
				else if(check==1){
					if(option==true) return -1;// fom doc ko tach dc
					else{
						if(count==0){// ko co fom ngag 4
							if(Math.abs(state[4]-z)>2){
								arrCardSV[tempFom[i][3]].methodRemove(state[4]);
								removeCardSV(arrCardSV[tempFom[i][3]], state[4]);
								arrCardSV[tempFom[i][3]].priority = 1;
								arrCardSH[state[1]].priority = 1;
							}else return -1;
						}else if(count==1){//co 1fom ngang4 va 1 fom ngang 3
							if(checkRemoveCardSV(sh4[4], arrCardSV[sh4[3]].arrCard.length)==false 
									&& sh4[0].take==true){
								// an vao card cua fom ngang 4 va card ko tach dc->ha ngang4 va doc
								arrCardSV[tempFom[i][3]].methodRemove(sh4[4]);
								removeCardSV(arrCardSV[tempFom[i][3]], sh4[4]);
								arrCardSV[tempFom[i][3]].priority = 1;
								arrCardSH[sh4[1]].priority = 1;
							}else if(sh3[0].take==true){
								//an vao card fom ngang 3--> ha 3 fom
								arrCardSV[tempFom[i][3]].methodRemove(sh3[4]);
								removeCardSV(arrCardSV[tempFom[i][3]], sh3[4]);
								arrCardSV[tempFom[i][3]].priority = 1;
								arrCardSH[sh4[1]].methodRemove(sh4[2]);
								arrCardSH[sh4[1]].priority = 1;
								arrCardSH[sh3[1]].priority = 1;
							}
						}
					}
				}else if(check>1){// ha het ngang
					arrCardSV[tempFom[i][3]].priority = 0;
					for(var j=i;j<tempFom.length;j++){
						if(tempFom[j][3] == tempFom[i][3]){
							arrCardSH[tempFom[j][1]].priority = 1;
						}
					}
				}
			}
			i=i+arrCardSV[tempFom[i][3]].findH();
			
		//fom ngang co >=2 con xep duoc fom doc, fom doc co >=2 con xep fom ngang
		}else if(arrCardSH[tempFom[i][1]].findV()>1 && arrCardSV[tempFom[i][3]].findH()>1){
			// chi co 2 loai la cap [2,2] va [3,3]
			var check=false;// fom ngang co card an ko fai card trung
			var takeHV = 0; // dem so cay an la card trung
			for(var j=i;j<arrCardSH[tempFom[i][1]].arrCard.length;j++){
				if(arrCardSH[tempFom[i][1]].arrCard[j].take==true 
						&& arrCardSH[tempFom[i][1]].arrCard[j].checkV==false){
					check==true; takeHV++;
				}
			}
			//ha fom khi co card trung V va H =3
			if(arrCardSH[tempFom[i][1]].findV()==3 && arrCardSV[tempFom[i][3]].findH()==3){
				if(check==true){
					for(var j=i;j<tempFom.length;j++){
						if(tempFom[j][3] == tempFom[i][3]){
							arrCardSH[tempFom[j][1]].priority = 1;
						}
					}
				}else{
					for(var j=i;j<tempFom.length;j++){
						if(tempFom[j][1] == tempFom[i][1]){
							arrCardSV[tempFom[j][3]].priority = 1;
						}
					}
				}
				break;
			}
			
			var count=0;// dem so fom ngang 4 cay(max =2 fom)
			var sh4 = null;// luu vi tri fom ngang 4 cay trong truong hop co count=1
			var sh3 = null;// luu vi tri fom ngang 3 cay trong truong hop co count=1
			for(var j=i;j<tempFom.length;j++){
				if(tempFom[j][3] == tempFom[i][3]){
					if(arrCardSH[tempFom[j][1]].arrCard.length == 4){
						count++; sh4 = tempFom[j];
					}
					else sh3 = tempFom[j];
				}
			}
			if(count==0 && i<tempFom.length-1){//ko co fom ngang 4
				var choice1 = priorityFom(arrCardSH[tempFom[i][1]], arrCardSV[tempFom[i][3]]);
				var choice2 = priorityFom(arrCardSH[tempFom[i+1][1]], arrCardSV[tempFom[i+1][3]]);
				if(choice1<=1 && choice2<=1){//ha ngagn
					for(var j=i;j<tempFom.length;j++){
						if(tempFom[j][3] == tempFom[i][3]){
							arrCardSH[tempFom[j][1]].priority = 1;
						}
					}
				}else if(choice1==2 && choice2==2){// ha doc
					for(var j=i;j<tempFom.length;j++){
						if(tempFom[j][1] == tempFom[i][1]){
							arrCardSV[tempFom[j][3]].priority = 1;
						}
					}
				}else if((choice1<=1||choice1>=3) && choice2>=2){
					if(check==true){//ha ngagn
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][3] == tempFom[i][3]){
								arrCardSH[tempFom[j][1]].priority = 1;
							}
						}
					}else{// ha doc
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][1] == tempFom[i][1]){
								arrCardSV[tempFom[j][3]].priority = 1;
							}
						}
					}
				}else if((choice2<=1||choice2>=3) && choice1>=2){
					if(check==true){// ha doc
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][1] == tempFom[i][1]){
								arrCardSV[tempFom[j][3]].priority = 1;
							}
						}
					}else{//ha ngagn
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][3] == tempFom[i][3]){
								arrCardSH[tempFom[j][1]].priority = 1;
							}
						}
					}
				}
			}else if(count==1){//1 fom ngag 4 va 1 fom ngag3+2fom doc
				var notVtakeH=0;//kiem tra xem 2 fom doc co cay an vao card trung ko?
				var fomVnotH=-1;//an 1 vao card trung thi la an fom doc bn
				for(var j=i;j<arrCardSV.length;j++){
					for(var k=0;k<arrCardSV[j].arrCard.lenght;k++){
						if(arrCardSV[j].arrCard[k].take==true && arrCardSV[j].arrCard[k].checkH==false){
							notVtakeH++;fomVnotH=j;break;
						}
					}
				}
				if(notVtakeH>1){//ha doc
					for(var j=i;j<tempFom.length;j++){
						if(tempFom[j][1] == tempFom[i][1]){
							arrCardSV[tempFom[j][3]].priority = 1;
						}
					}
				}else{
					if(sh4[4]>sh3[4]&&checkRemoveCardSV(sh3[4], arrCardSV[sh3[3]].arrCard.length)==true&&sh3[3]!=fomVnotH){
						arrCardSH[sh3[1]].priority = 1;
						arrCardSH[sh4[1]].methodRemove(sh4[2]);
						arrCardSH[sh4[1]].priority = 1;
						arrCardSV[sh3[3]].methodRemove(sh3[4]);
						arrCardSV[sh3[3]].priority = 1;
					}else if(notVtakeH==1){
						arrCardSV[fomVnotH].priority = 1;
						arrCardSH[sh4[1]].methodRemove(fomVnotH);
						var fomV=-1;
						if(fomVnotH==0) fomV=1;
						else fomV=0;
						var choice = priorityFom(arrCardSH[sh4[1]], arrCardSV[fomV]);
						if(choice<=1) arrCardSH[sh4[1]].priority = 1;
						else arrCardSV[fomV].priority = 1;
					}else if(notVtakeH==0){// ha ngang
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][3] == tempFom[i][3]){
								arrCardSH[tempFom[j][1]].priority = 1;
							}
						}
					}
				}
				
			}else if(count==2){//2 fom ngang4+2fom doc 3
				var notVtakeH=0;//kiem tra xem 2 fom doc co cay an vao card trung ko?
				var fom=-1;//an 1 vao card trung thi la an fom doc bn
				for(var j=i;j<arrCardSV.length;j++){
					for(var k=0;k<arrCardSV[j].arrCard.length;k++){
						if(arrCardSV[j].arrCard[k].take==true && arrCardSV[j].arrCard[k].checkH==false){
							notVtakeH++;fom=j;break;
						}
					}
				}
				if(notVtakeH>1){//ha doc
					for(var j=i;j<tempFom.length;j++){
						if(tempFom[j][1] == tempFom[i][1]){
							arrCardSV[tempFom[j][3]].priority = 1;
						}
					}
				}else{
					if(fom==-1){
						for(var j=i;j<tempFom.length;j++){
							if(tempFom[j][1] == tempFom[i][1]){
								if(arrCardSV[tempFom[j][3]].findTake()==2){
									if(tempFom[j][3]==0) fom=1;
									else fom=0;
								}else fom=0;
								break;
							}
						}
					}
					arrCardSV[fom].priority = 1;
					for(var j=i;j<tempFom.length;j++){
						if(tempFom[j][3] == tempFom[i][3]){
							arrCardSH[tempFom[j][1]].methodRemove(fom);
							arrCardSH[tempFom[j][1]].priority = 1;
						}
					}
				}
			}
			i=i+tempFom.length;
			
		//fom chi co 1 card trung jua ngang va doc
		}else if(arrCardSH[tempFom[i][1]].findV()==1 && arrCardSV[tempFom[i][3]].findH()==1){
			if(arrCardSH[tempFom[i][1]].arrCard.length==3 && 
					checkRemoveCardSV(tempFom[i][4], arrCardSV[tempFom[i][3]].arrCard.length)==false){
				var choice = priorityFom(arrCardSH[tempFom[i][1]], arrCardSV[tempFom[i][3]]);
				if(choice<=1) arrCardSH[tempFom[i][1]].priority = 1;
				else if(choice==2) arrCardSV[tempFom[i][3]].priority = 1;
				else if(choice>2){
					if(tempFom[i][0].take==true){
						if(choice==3) arrCardSH[tempFom[i][1]].priority = 1;
						if(choice==4) arrCardSV[tempFom[i][3]].priority = 1;
					}else return -1;
				}
			}else if(arrCardSH[tempFom[i][1]].arrCard.length==4 && 
					checkRemoveCardSV(tempFom[i][4], arrCardSV[tempFom[i][3]].arrCard.length)==false){
				arrCardSH[tempFom[i][1]].methodRemove(tempFom[i][2]);
				arrCardSH[tempFom[i][1]].priority = 1;
				arrCardSV[tempFom[i][3]].priority = 1;
			}else if(arrCardSH[tempFom[i][1]].arrCard.length>=3 && 
					checkRemoveCardSV(tempFom[i][4], arrCardSV[tempFom[i][3]].arrCard.length)==true){
				arrCardSV[tempFom[i][3]].methodRemove(tempFom[i][4]);
				removeCardSV(arrCardSV[tempFom[i][3]], tempFom[i][4]);
				arrCardSV[tempFom[i][3]].priority = 1;
				arrCardSH[tempFom[i][1]].priority = 1;
			}
		}
	}
	
	arrCardState = new Array();
	//dem so cay an
	var countTake = 0;
	var flagTake = true;//so cay an trong fom>2 -->false
	for(var i=0;i<arrCardSH.length;i++){
		if(arrCardSH[i].priority == 1){
			arrCardState.push(arrCardSH[i].arrCard);
			countTake = countTake+arrCardSH[i].findTake();
			if(arrCardSH[i].findTake()>1) flagTake = false;
		}
	}
	for(var i=0;i<arrCardSV.length;i++){
		if(arrCardSV[i].priority == 1){
			arrCardState.push(arrCardSV[i].arrCard);
			countTake = countTake+arrCardSV[i].findTake();
			if(arrCardSV[i].findTake()>1) flagTake = false;
		}
	}

	var totalFom = arrCardState.length;
	//kiem tra xem co bao nhieu fom
	if(countTake!=objCardMe.findTake() || totalFom<objCardMe.findTake() || flagTake==false){
		return -1;//AN LAO
	}else if(totalFom == 0){
		return 0;
	}else if(totalFom == 3){
		if(objCardMe.findTake()==3) return 100;
		else return 10;
	}else if(totalFom != 0){
		return 1;
	}
}

// Function manual ha bai
function manualSetState(){
	var tempArr = new Array();
	for(var i=0;i<objCardMe.arrCard.length;i++){
		if(objCardMe.arrCard[i].click == 1){
			tempArr.push(objCardMe.arrCard[i]);
		}
	}
	if(tempArr.length<3 || tempArr.length>5) return;
	var check=false;
	if(parseInt(tempArr[0].num[0])==parseInt(tempArr[1].num[0]) &&
			parseInt(tempArr[1].num[0])==parseInt(tempArr[2].num[0])){
		check=true;
		if(tempArr.length==4){
			if(parseInt(tempArr[2].num[0])!=parseInt(tempArr[3].num[0])){
				check=false;
			}
		}else if(tempArr.length>4) check=false;
	}else if(parseInt(tempArr[0].suit[0]) == parseInt(tempArr[1].suit[0]) &&
				parseInt(tempArr[1].suit[0]) == parseInt(tempArr[2].suit[0])){
		var a = tempArr[0].num[0];
		var b = tempArr[1].num[0];
		var c = tempArr[2].num[0];
		if(a==b-1 && b==c-1){
			check=true;
			if(tempArr.length>3){
				for(var i=3;i<tempArr.length;i++){
					if(parseInt(tempArr[2].suit[0]) == parseInt(tempArr[i].suit[0]) &&
							c==tempArr[i].num[0]-1){
						check=true;c=tempArr[i].num[0];
					}else{
						check=false;break;
					}
				}
			}
		}
	}
	if(check==true){
		arrCardState.push(tempArr);
		manualState++;
		for(var i=1;i<=objCardMe.arrCard.length;i++){
			removeElement('cardBot','cardBot_'+i+'');
		}
		//	Xoa bo cac thanh phan trong objCardMe va hien thi
		for(var j=0; j<arrCardState.length;j++){
			for(var k=0;k<arrCardState[j].length;k++){
				for(var i=0;i<objCardMe.arrCard.length;i++){
					if(objCardMe.arrCard[i].num[0] == arrCardState[j][k].num[0] && 
							objCardMe.arrCard[i].suit[0] == arrCardState[j][k].suit[0]){
					objCardMe.methodRemove(i);
					break;
					}
				}
			}
		}
		for(var i=0;i<objCardMe.arrCard.length;i++){
			var j = i+1;
			var _div = document.createElement('div');
			_div.setAttribute('id','cardBot_'+j+'');
			if(objCardMe.arrCard[i].take == true){
				_div.setAttribute('class','cardTake');
			}else _div.setAttribute('class','card');
			_div.setAttribute('onclick','clickCard(this)');
			_div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
			document.getElementById("cardBot").appendChild(_div);		
			var _span = document.createElement('span');
			_span.setAttribute('id','span'+j+'');
			_span.setAttribute('class','spanNum');
			_span.style.backgroundColor = '#fff';
			_span.style.color = objCardMe.arrCard[i].suit[2];
			document.getElementById('cardBot_'+j+'').appendChild(_span);
			var _txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
			document.getElementById('span'+j+'').appendChild(_txt);
			if(objCardMe.arrCard[i].take == true){
				new Motion(_div,[0,0],[9.5*i,-2],[12.5,100],[12.5,104],5+i*5);
			}else new Motion(_div,[0,0],[9.5*i,0],[12.5,100],[12.5,100],5+i*5);
			_div.style.display = 'block';
			_div.style.zIndex = ''+j+'';
		}
		//	Hien thi ra cho ha bai
		document.getElementById('downBot').style.display = 'block';
		var div = document.createElement('div');
		div.setAttribute('id','downBot_state'+manualState+'');
		div.setAttribute('class','downState');
		div.style.display = 'block';
		var top = -25*(manualState-1);
		div.style.top = ''+top+'%';
		div.style.zIndex = ''+manualState+'';
		document.getElementById("downBot").appendChild(div);
		for(var j=0;j<arrCardState[manualState-1].length;j++){
			var b =j+1;
			var _div = document.createElement('div');
			_div.setAttribute('id', 'downBot_state'+manualState+b);
			_div.setAttribute('class', 'card');
			_div.style.backgroundImage='url('+arrCardState[manualState-1][j].suit[1]+')';
			_div.style.display = 'block';
			_div.style.zIndex = ''+b+'';
			div.appendChild(_div);
			var span = document.createElement('span');
			span.setAttribute('id','spanB_state'+manualState+b);
			span.setAttribute('class','spanNum');
			span.style.backgroundColor = '#fff';
			span.style.color = arrCardState[manualState-1][j].suit[2];
			var txt = document.createTextNode(arrCardState[manualState-1][j].num[1]);
			span.appendChild(txt);
			_div.appendChild(span);
			new Motion(_div,[0,0],[17*j,0],[26,99],[26,99],100);
		}
	}
	else return;
}

// Hien thi ha fom cua Me
function displayDownCard(){
	for(var i=1;i<=objCardMe.arrCard.length;i++){
		removeElement('cardBot','cardBot_'+i+'');
	}
	//	Xoa bo cac thanh phan trong objCardMe va hien thi
	for(var j=0; j<arrCardState.length;j++){
		for(var k=0;k<arrCardState[j].length;k++){
			for(var i=0;i<objCardMe.arrCard.length;i++){
				if(objCardMe.arrCard[i].num[0] == arrCardState[j][k].num[0] && 
						objCardMe.arrCard[i].suit[0] == arrCardState[j][k].suit[0]){
					objCardMe.methodRemove(i);
					break;
				}
			}
		}
	}
	for(var i=0;i<objCardMe.arrCard.length;i++){
		var j = i+1;
		var _div = document.createElement('div');
		_div.setAttribute('id','cardBot_'+j+'');
		_div.setAttribute('class','card');
		_div.setAttribute('onclick','clickCard(this)');
		_div.style.backgroundImage='url('+objCardMe.arrCard[i].suit[1]+')';
		document.getElementById("cardBot").appendChild(_div);		
		var _span = document.createElement('span');
		_span.setAttribute('id','span'+j+'');
		_span.setAttribute('class','spanNum');
		_span.style.backgroundColor = '#fff';
		_span.style.color = objCardMe.arrCard[i].suit[2];
		document.getElementById('cardBot_'+j+'').appendChild(_span);
		var _txt = document.createTextNode(objCardMe.arrCard[i].num[1]);
		document.getElementById('span'+j+'').appendChild(_txt);
		new Motion(_div,[0,0],[9.5*i,0],[12.5,100],[12.5,100],10+i*5);
		_div.style.display = 'block';
		_div.style.zIndex = ''+j+'';
	}
	
	//	Hien thi ra cho ha bai
	document.getElementById('downBot').style.display = 'block';
	for(var i=0; i<arrCardState.length;i++){
		var a = i+1;
		var div = document.createElement('div');
		div.setAttribute('id','downBot_state'+a+'');
		div.setAttribute('class','downState');
		div.style.display = 'block';
		var top = -25*i;
		div.style.top = ''+top+'%';
		div.style.zIndex = ''+a+'';
		document.getElementById("downBot").appendChild(div);
		for(var j=0;j<arrCardState[i].length;j++){
			var b =j+1;
			var _div = document.createElement('div');
			_div.setAttribute('id', 'downBot_state'+a+b);
			_div.setAttribute('class', 'card');
			_div.style.backgroundImage='url('+arrCardState[i][j].suit[1]+')';
			_div.style.display = 'block';
			_div.style.zIndex = ''+b+'';
			div.appendChild(_div);
			var span = document.createElement('span');
			span.setAttribute('id','spanB_state'+a+b);
			span.setAttribute('class','spanNum');
			span.style.backgroundColor = '#fff';
			span.style.color = arrCardState[i][j].suit[2];
			var txt = document.createTextNode(arrCardState[i][j].num[1]);
			span.appendChild(txt);
			_div.appendChild(span);
			new Motion(_div,[0,0],[17*j,0],[26,99],[26,99],100);
		}
	}
}

// Function lay thong tin hạ bài cua cac gamer # tu server va hien thi
function getDownCard(){
 
}


/******************** BOC BAI ***********************/
// Function click boc bai cua Me
function clickAddCard(){
	setAddCard();
}

// Function bốc bài
function setAddCard(){
	// Add vao objCardMe quan bai duoc gui tu server ve
	objCardMe.methodAdd(new objCard(1,4));
	// Hien thi bai an ve bai cua minh
	var index=objCardMe.arrCard.length;
	var _div = document.createElement('div');
	_div.setAttribute('id','cardBot_'+index);
	_div.setAttribute('class','card');
	_div.setAttribute('onclick','clickCard(this)');
	_div.style.backgroundImage='url('+objCardMe.arrCard[index-1].suit[1]+')';
	document.getElementById("cardBot").appendChild(_div);		
	var _span = document.createElement('span');
	_span.setAttribute('id','span'+index);
	_span.setAttribute('class','spanNum');
	_span.style.backgroundColor = '#fff';
	_span.style.color = objCardMe.arrCard[index-1].suit[2];
	document.getElementById('cardBot_'+index).appendChild(_span);
	var _txt = document.createTextNode(objCardMe.arrCard[index-1].num[1]);
	document.getElementById('span'+index).appendChild(_txt);
	new Motion(_div,[50,-225],[9.5*(index-1),0],[0,0],[12.5,100],100);
	_div.style.display = 'block';
	_div.style.zIndex = ''+index+'';
}


/******************** AN BAI ***********************/
// Function click de an bai cua Me
function clickTakeCard(){
	setTakeCard();
}

// Function ăn bài cua Me
function setTakeCard(){
	var _round = round+1;
	// Hien thi remove bai o arrCardGamble[max];
	if(currTbl.numPlayer == 2 || (currTbl.numPlayer==3 && currPlayer.isCreateTbl==true)){
		removeElement('gamerTop_gamble','gambleTop_'+_round+'');
	}else{
		removeElement('gamerLeft_gamble','gambleLeft_'+_round+'');
	}
	// remove obj arrCardGamble[max].arrCard[round] va add obj card vao objCardMe;
	objCardMe.methodAdd(arrCardGamble[totalGamer-1].arrCard[round]);
	objCardMe.setTake(objCardMe.arrCard.length-1);
	arrCardGamble[totalGamer-1].methodRemove(round);
	
	// Hien thi bai an ve bai cua minh
	var _span = document.createElement('span');
	_span.setAttribute('id','span10');
	_span.setAttribute('class','spanNum');
	_span.style.backgroundColor = '#fff';
	_span.style.color = objCardMe.arrCard[9].suit[2];
	var _txt = document.createTextNode(objCardMe.arrCard[9].num[1]);
	_span.appendChild(_txt);	
	var div = document.createElement('div');
	div.setAttribute('id','cardBot_10');
	div.setAttribute('class','cardTake');
	div.setAttribute('onclick','clickCard(this)');
	var _div = document.createElement('div');
	_div.setAttribute('class','card');
	_div.style.backgroundImage='url('+objCardMe.arrCard[9].suit[1]+')';
	_div.style.display = 'block';	
	_div.appendChild(_span);
	div.appendChild(_div);
	document.getElementById("cardBot").appendChild(div);
	new Motion(div,[24,-22],[9.5*9,-6],[12.5,100],[14,108],100);
	div.style.display = 'block';
	div.style.zIndex = '10';
}

// Function lay thong tin an bai cua thag # dc gui tu server
function getTakeCard(){

}


/******************** GUI BAI ***********************/
// Function gửi bài
function attachCard(){
 
}





